package com.ysm.smfu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ysm.smfu.R;
import com.ysm.smfu.bean.PayResult;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.manager.GlideCircleTransform;
import com.ysm.smfu.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/15.
 */
public class PaySuccessFragment extends MyBaseFragment {

    @BindView(R.id.fragment_success_img)
    ImageView fragmentSuccessImg;
    @BindView(R.id.nun)
    TextView nun;
    @BindView(R.id.success_text)
    TextView success_text;
    private PayResult payResult;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.success_pay);
        ButterKnife.bind(this, view);
        sendRroadCastForFinish();
        Bundle arguments = getArguments();
        if (arguments != null)
            payResult = (PayResult) arguments.getSerializable(SpKey.PAYRESULT);
        if (payResult != null) {
            if (!TextUtils.isEmpty(payResult.headPhone) && !payResult.headPhone.startsWith("null?"))
                Glide.with(PaySuccessFragment.this).load(payResult.headPhone).placeholder(R.drawable.myimg2).error(R.drawable.myimg2)
                        .transform(new GlideCircleTransform(UIUtils.getContext()))
                        .into(fragmentSuccessImg);

            if (TextUtils.equals(payResult.points, "0") || TextUtils.isEmpty(payResult.points) || TextUtils.equals(payResult.points, "null")) {
                nun.setText("-" + payResult.price + "元");
                success_text.setVisibility(View.VISIBLE);
                if (!TextUtils.equals(payResult.returnPoints, "0")) {
                    success_text.setText("恭喜您获得" + payResult.returnPoints + "兑换券!");
                } else {
                    success_text.setText("恭喜!");
                }
            } else {
                success_text.setVisibility(View.VISIBLE);
                nun.setText("-" + payResult.price + "元" + "     " + payResult.points + "兑换券");
                success_text.setVisibility(View.VISIBLE);
                if (!TextUtils.equals(payResult.returnPoints, "0")) {
                    success_text.setText("恭喜您获得" + payResult.returnPoints + "兑换券！");
                } else {
                    success_text.setText("恭喜！");
                }
            }
        }
        return view;
    }

    @OnClick(R.id.fragment_shoukuan_pay)
    public void onClick() {
        getActivity().finish();
    }

    /**
     * 发送广播结束"输入金额页面??"和输入密码页面
     */
    private void sendRroadCastForFinish() {
        Intent intent = new Intent();
        intent.setAction("finfisPrepay");
        getActivity().sendBroadcast(intent);
    }
}
